package models

import (
	"errors"
	"github.com/astaxie/beego/orm"
	"github.com/astaxie/beego/validation"
	"log"
	"time"
)

type Article struct {
	Id         int64
	Title      string `orm:"uinque;size(60)" form:"title" valid:"Required;MinSize(6);MaxSize(20)"`
	Intro      string `orm:"size(120)" form:"intro" valid:"Required;MinSize(10);MaxSize(120)"`
	Content    string `orm:"type(text)" form:content valid:"Required"`
	TypeId     int32  `orm:"size(8)" form:"typeId" valid:"Required"`
	StatusIs   int32  `orm:"size(1)" form:"statusIs"`
	ViewTimes  int32  `orm:"size(11)" form:"viewTimes"`
	AdminId    int32  `orm:"size(10)" form:"adminId" valid:"Required"`
	CreateTime int64  `orm:"size(10)" valid:"Required"`
	UpdateTime int64  `orm:"size(10)" valid:"Required"`
}

//自定义模型(表名)
func (a *Article) TableName() string {
	return "yii2_article"
}

//初始化
func init() {
	orm.RegisterModel(new(Article))
}

//========================================//
//验证文章信息
func checkArticle(a *Article) (err error) {
	valid := validation.Validation{}
	b, _ := valid.Valid(&a)
	if !b {
		for _, err := range valid.Errors {
			log.Println(err.Key, err.Message)
			return errors.New(err.Message)
		}
	}
	return nil
}

//获取文章列表
func GetArticleList(typeId int64, page int64, pageSize int64, sort string) (articals []orm.Params, count int64) {
	o := orm.NewOrm()
	article := new(Article)
	qs := o.QueryTable(article)
	var offset int64
	if page <= 1 {
		offset = 0
	} else {
		offset = (page - 1) * pageSize
	}
	if typeId > 0 {
		qs = qs.Filter("type_id", typeId)
	}
	qs = qs.Filter("status_is", 1)
	qs.Limit(pageSize, offset).OrderBy(sort).Values(&articals)
	count, _ = qs.Count()
	return articals, count

}

//获取热门文章列表
func GetHotList() (articals []orm.Params) {
	o := orm.NewOrm()
	article := new(Article)
	qs := o.QueryTable(article)
	qs.Limit(10, 0).OrderBy("id").Values(&articals)
	return articals

}

//新增文章
func AddArticle(a *Article) (int64, error) {

	if err := checkArticle(a); err != nil {
		return 0, err
	}

	o := orm.NewOrm()
	article := new(Article)
	article.Title = a.Title
	article.Intro = a.Intro
	article.Content = a.Content
	article.TypeId = a.TypeId
	article.AdminId = a.AdminId
	article.CreateTime = time.Now().Unix()
	article.UpdateTime = time.Now().Unix()
	id, err := o.Insert(article)
	return id, err

}

//更新文章
func UpdateArticle(a *Article) (int64, error) {
	if err := checkArticle(a); err != nil {
		return 0, err
	}
	o := orm.NewOrm()
	article := make(orm.Params)
	if len(a.Title) > 0 {
		article["Title"] = a.Title
	}
	if len(a.Content) > 0 {
		article["Content"] = a.Content
	}
	if len(article) == 0 {
		return 0, errors.New("update field is empty")
	}
	var table Article
	num, err := o.QueryTable(table).Filter("Id", a.Id).Update(article)
	return num, err
}

//根据id删除文章
func DelArticleById(Id int64) (int64, error) {
	o := orm.NewOrm()
	status, err := o.Delete(&Article{Id: Id})
	return status, err
}

//获取单篇文章
func GetArticleById(Id int64) (art Article) {
	art = Article{Id: Id}
	o := orm.NewOrm()
	o.Read(&art, "Id")

	return art
}
